import {
  showText
} from '../../utils/util'
import {
  getShopList
} from '@/api/spa'
import {
  regionData as arrayList
} from '@/utils/element-china-area-data'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    cardIndex: -1,
    //dataArr: [1, 2, 3, 4, 5, 6, 7],
    dataArr: [],
    pageNum: 1,
    pageSize: 10,
    isEnd: false,
    spaId: 1,
    absId: 1,
    latitude: 0,
    longitude: 0,
    multiIndex: [0, 0, 0],
    multiIndex1: [0, 0, 0],
    areaArray: [],
    areaArray1: [],
    // 片区数据
    districtArray: [],
    districtIndex: 0,
    navbar: [{
        label: '距离排序',
        value: 1,
      },
      {
        label: '热门门店',
        value: 2,
      },
      {
        label: '我的收藏',
        value: 3,
      },
    ],
    navbarIndex: 0,
    sortType: 1, // 1 距离 2 热门

    province: '',
    city: '',
    area: '',

    province1: '',
    city1: '',
    area1: '',
  },
  bindDistrictChange(e) {
    console.log('单选联动控制', e)
    this.setData({
        area: this.data.districtArray[e.detail.value].label,
      },
      () => {
        this.getShopList()
      }
    )
  },
  bindMultiPickerColumnChange(e) {
    var that = this
    console.log('多级联动控制', e)
    //获取当前滚动的是哪一列，通过判断哪一列来实时控制下一列的变化
    console.log(
      '第',
      e.detail.column + 1,
      '列。第',
      e.detail.value + 1,
      '个数据'
    )
    switch (e.detail.column) {
      case 0: //第一列
        var list2 = [],
          list3 = []
        //选择第一列的这个值然后找到它的儿子
        list2 = that.data.areaArray[0][e.detail.value].children
        //实时修改第二列，并且把第二列设置到第一个展示
        list3 = list2[0].children
        that.setData({
          'areaArray[1]': list2,
          // districtArray: list3,
          'multiIndex[0]': e.detail.value,
          'multiIndex[1]': 0,
          'multiIndex[2]': 0,
        })
        break
      case 1: //第二列
        var list3 = []
        //从所有第二列的内容中取出与第一列选中状态的值对应的数据用于实时生成第二列  ？？？？？
        list3 = that.data.areaArray[1][e.detail.value].children
        that.setData({
          // districtArray: list3,
          'multiIndex[1]': e.detail.value,
          'multiIndex[2]': 0,
        })
        break
      case 2: //第三列
        that.setData({
          'multiIndex[2]': e.detail.value,
        })
        break
    }
  },

  bindAreaChange(e) {
    let multiIndex = this.data.multiIndex
    console.log(multiIndex)
    // 处理片区数据
    let districtArray =
      arrayList[multiIndex[0]].children[multiIndex[1]].children
    console.log(arrayList[multiIndex[0]].children[multiIndex[1]].children)
    this.setData({
        province: arrayList[multiIndex[0]].label,
        city: arrayList[multiIndex[0]].children[multiIndex[1]].label,
        // area: arrayList[multiIndex[0]].children[multiIndex[1]].children[multiIndex[2]]
        //   .label,
        districtArray,
        districtIndex: 0,
        area: ''
      },
      () => {
        this.getShopList()
      }
    )
  },

  bindMultiPickerColumnChange1(e) {
    var that = this
    console.log('多级联动控制', e)
    //获取当前滚动的是哪一列，通过判断哪一列来实时控制下一列的变化
    console.log(
      '第',
      e.detail.column + 1,
      '列。第',
      e.detail.value + 1,
      '个数据'
    )
    switch (e.detail.column) {
      case 0: //第一列
        var list2 = [],
          list3 = []
        //选择第一列的这个值然后找到它的儿子
        list2 = that.data.areaArray1[0][e.detail.value].children
        //实时修改第二列，并且把第二列设置到第一个展示
        list3 = list2[0].children
        that.setData({
          'areaArray1[1]': list2,
          'areaArray1[2]': list3,
          'multiIndex1[0]': e.detail.value,
          'multiIndex1[1]': 0,
          'multiIndex1[2]': 0,
        })
        break
      case 1: //第二列
        var list3 = []
        //从所有第二列的内容中取出与第一列选中状态的值对应的数据用于实时生成第二列  ？？？？？
        list3 = that.data.areaArray1[1][e.detail.value].children
        that.setData({
          'areaArray1[2]': list3,
          'multiIndex1[1]': e.detail.value,
          'multiIndex1[2]': 0,
        })
        break
      case 2: //第三列
        that.setData({
          'multiIndex1[2]': e.detail.value,
        })
        break
    }
  },

  bindAreaChange1(e) {
    let multiIndex = this.data.multiIndex1
    this.setData({
        province1: arrayList[multiIndex[0]].label,
        city1: arrayList[multiIndex[0]].children[multiIndex[1]].label,
        area1: arrayList[multiIndex[0]].children[multiIndex[1]].children[
          multiIndex[2]
        ].label,
      },
      () => {
        this.getShopList()
      }
    )
    console.log(this.data.province1, this.data.city1, this.data.area1)
  },
  todetail(e) {
    let {
      absid,
      index,
      spabooking
    } = e.currentTarget.dataset

    if (spabooking == false) {
      return false
    }
    this.setData({
        cardIndex: index,
      },
      () => {
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/storyDetail/storyDetail?latitude=' +
              this.data.latitude +
              '&longitude=' +
              this.data.longitude +
              '&spaId=' +
              this.data.spaId +
              '&absId=' +
              absid,
          })
        }, 100)
      }
    )
  },
  // 清除省市区
  clearRegion(){
    this.setData({
      province:'',
      city:'',
      area:''
    },()=>{
      this.getShopList()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      areaArray: [
        arrayList,
        arrayList[0].children,
        // arrayList[0].children[0].children,
      ],
      areaArray1: [
        arrayList,
        arrayList[0].children,
        arrayList[0].children[0].children,
      ],
    })

    let t = this
    if (options.spaId) {
      var spaId = options.spaId
      var latitude = options.latitude
      var longitude = options.longitude
      t.setData({
        spaId: spaId,
        latitude: latitude,
        longitude: longitude,
      })
      this.getShopList()
    }
  },
  changeNavbar(e) {
    let {
      index
    } = e.currentTarget.dataset
    //console.log(index+"==="+this.data.navbarIndex);
    if (index == this.data.navbarIndex) {
      return false
    }
    const value = this.data.navbar[index].value
    this.setData({
      navbarIndex: index,
      sortType: value,
      isEnd: false,
      dataArr: [],
      pageNum: 1,
      cardIndex: -1
    })
    console.log(value, '切换后的')
    this.getShopList()
  },

  getShopList(pageNum = 1) {
    let t = this
    if (pageNum == 1) {
      t.setData({
        dataArr: [],
        isEnd: false,
      })
    }
    let p = {
      latitude: t.data.latitude,
      longitude: t.data.longitude,
      pageNum,
      pageSize: t.data.pageSize,
      spaId: t.data.spaId,
      sortType: t.data.sortType,
      // province: this.data.navbarIndex == 0 ? this.data.province : this.data.province1,
      // city: this.data.navbarIndex == 0 ? this.data.city : this.data.city1,
      // district: this.data.navbarIndex == 0 ? this.data.area : this.data.area1,
      province: t.data.province,
      city: t.data.city,
      district: t.data.area,
    };
    if(t.data.navbarIndex == 2){
      p.collected = true;
    }
    getShopList(p).then((res) => {
      if (res.code == 200) {
        let data = res.data.listData
        for (var i = 0; i < data.length; i++) {
          if (data[i].distance) {
            var distance = data[i].distance
            data[i].distance = Math.round(distance / 1000)
          }
        }
        let list = this.data.dataArr.concat(data)
        let isEnd = list.length >= res.data.total
        this.setData({
          dataArr: list,
          isEnd,
        })
      }
    })
  },
  bindscrolltolower() {
    console.log(123)
    let {
      isEnd
    } = this.data
    if (!isEnd) {
      let pageNum = Math.ceil(this.data.dataArr.length / this.data.pageSize) + 1
      this.getShopList(pageNum)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})